package fun.cyhgraph.controller.merchant;

import fun.cyhgraph.annotation.AuthCheck;
import fun.cyhgraph.common.ErrorCode;
import fun.cyhgraph.common.UserConstant;
import fun.cyhgraph.exception.ThrowUtils;
import fun.cyhgraph.model.dto.DishDTO;
import fun.cyhgraph.model.dto.DishPageDTO;
import fun.cyhgraph.model.entity.MerchantInfo;
import fun.cyhgraph.model.entity.User;
import fun.cyhgraph.model.merchantDto.DishMerchantDTO;
import fun.cyhgraph.model.vo.DishVO;
import fun.cyhgraph.result.PageResult;
import fun.cyhgraph.result.Result;
import fun.cyhgraph.service.DishService;
import fun.cyhgraph.service.MerchantInfoService;
import fun.cyhgraph.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/merchant/dish")
@Tag(name = "菜品管理")
@Slf4j
public class DishController {

    @Resource
    private DishService dishService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserService userService;
    @Resource
    private MerchantInfoService merchantInfoService;

    /**
     * 添加菜品
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "添加菜品")
    @PostMapping
    public Result addDishWithFlavor(@RequestBody DishDTO dishDTO , HttpServletRequest request){
        ThrowUtils.throwIf(dishDTO == null , ErrorCode.PARAMS_ERROR);
        if (dishDTO.getMerchantId() == null) {
            User user = userService.getLoginUser(request);
            dishService.addDishWithFlavor(dishDTO , user.getId());
        }else {
            dishService.addDishWithFlavor(dishDTO , dishDTO.getMerchantId());
        }
        return Result.success("新增成功");
    }

    /**
     * 菜品条件分页查询
     * @param dishPageDTO
     * @return
     */
    @Operation(summary = "菜品分页查询")
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @PostMapping("/page")
    public Result<PageResult> getPageList(@RequestBody DishPageDTO dishPageDTO , HttpServletRequest request){
        ThrowUtils.throwIf(dishPageDTO == null , ErrorCode.PARAMS_ERROR);
        if (dishPageDTO.getMerchantId() == null){
            PageResult pageResult = dishService.getPageList(dishPageDTO , null);
            return Result.success(pageResult);
        }else {
            PageResult pageResult = dishService.getPageList(dishPageDTO , dishPageDTO.getMerchantId());
            return Result.success(pageResult);
        }


    }

    /**
     * 根据id查询菜品和对应口味
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询菜品和对应口味")
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @GetMapping("/{id}")
    public Result<DishVO> getDishWithFlavorById(@PathVariable Integer id  , @RequestParam(required = false) Integer merchantId, HttpServletRequest request){
        if (merchantId == null){
            DishVO dishVO = dishService.getDishWithFlavorById(id , null);
            return Result.success(dishVO);
        }else {
            DishVO dishVO = dishService.getDishWithFlavorById(id , merchantId);
            return Result.success(dishVO);
        }


    }

    /**
     * 根据id修改起售停售状态
     * @param id
     * @return
     */
    @Operation(summary = "根据id修改起售停售状态")
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @PutMapping("/status/{id}")
    public Result onOff(@PathVariable Integer id ,@RequestParam(required = false) Integer merchantId, HttpServletRequest request){
        if (merchantId == null){
            dishService.onOff(id , null);
        }else {
            dishService.onOff(id , merchantId);
        }
        return Result.success("修改成功");
    }

    /**
     * 更新菜品以及对应口味
     * @param dishDTO
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "更新菜品以及对应口味")
    @PutMapping
    public Result updateDishWithFlavor(@RequestBody DishDTO dishDTO , HttpServletRequest request){
        ThrowUtils.throwIf(dishDTO == null , ErrorCode.PARAMS_ERROR);
        DishMerchantDTO dishMerchantDTO = new DishMerchantDTO();
        BeanUtils.copyProperties(dishDTO, dishMerchantDTO);
        merchantInfoService.judgeMerchantInfo(dishDTO.getMerchantId());
        dishMerchantDTO.setMerchantId(dishDTO.getMerchantId());
        dishService.updateDishWithFlavor(dishMerchantDTO);
        return Result.success("更新成功");
    }

    /**
     * 根据ids批量删除菜品数据
     * @RequestParam 表示必须要ids参数，否则会报错
     * @param ids
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "根据ids批量删除菜品数据")
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Integer> ids  , @RequestParam(required = false) Integer merchantId, HttpServletRequest request){
        if (merchantId == null){
            User loginUser = userService.getLoginUser(request);
            dishService.deleteBatch(ids , loginUser.getId());
        }else {
            dishService.deleteBatch(ids , merchantId);
        }
        return Result.success();
    }
    /**
     * 查询所有商铺信息
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @Operation(summary = "查询所有商铺信息")
    @GetMapping("/allMerchant")
    public Result<List<MerchantInfo>> getAllMerchant(){
        List<MerchantInfo> list = merchantInfoService.list();
        return Result.success(list);
    }
    /**
     * 清理缓存数据
     * @param pattern
     */
    public void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
